package com.dx.architectureddd.app.executor;

import com.dx.architectureddd.api.dto.req.FinishCmd;
import com.dx.architectureddd.app.chain.init.InitContext;
import com.dx.architectureddd.app.chain.third.ThirdContext;
import com.dx.architectureddd.client.dto.event.JoinGameEvent;
import com.dx.architectureddd.common.dp.pipeline.PipelineFactory;
import com.dx.architectureddd.common.event.impl.DomainSpringEventPublisher;
import com.dx.architectureddd.domain.ThirdServiceEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 初始化执行器
 *
 * @author xin.deng
 * @date 2023/10/05
 */
@Slf4j
@Component
public class InitCmdExe {
    @Resource
    private PipelineFactory<InitContext> initContextPipelineFactory;
    @Resource
    private PipelineFactory<ThirdContext> thirdContextPipelineFactory;
    @Resource
    private DomainSpringEventPublisher domainSpringEventPublisher;

    public FinishCmd execute(String level) {
        InitContext initContext = new InitContext();
        Map<String, Object> inPut = new HashMap<>(5);
        inPut.put("level", level);
        initContext.setInPut(inPut);
        // 链处理开始
        initContextPipelineFactory.acceptSync(initContext);
        FinishCmd finishCmd = initContext.getFinishCmd();
        if (ObjectUtils.isEmpty(finishCmd)) {
            return null;
        }
        ThirdContext thirdContext = new ThirdContext();
        String reward = finishCmd.getReward();
        thirdContext.setStrategyType(StringUtils.isEmpty(reward) ? ThirdServiceEnum.UPLOAD.getValue() : ThirdServiceEnum.PAYMENT.getValue());
        log.info("thirdContext:{}", thirdContext);
        thirdContextPipelineFactory.acceptSync(thirdContext);
        finishCmd.setLevel(level);
        domainSpringEventPublisher.publishEvent(new JoinGameEvent(level));
        return finishCmd;
    }
}
